/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package deprecated;

import com.jme3.export.InputCapsule;
import com.jme3.export.JmeExporter;
import com.jme3.export.JmeImporter;
import com.jme3.export.OutputCapsule;
import com.jme3.math.FastMath;
import com.jme3.math.Vector2f;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Geometry;
import com.jme3.scene.Spatial;
import com.jme3.scene.VertexBuffer.Type;
import com.jme3.scene.control.BillboardControl;
import com.jme3.scene.control.Control;
import com.jme3.util.BufferUtils;
import java.io.IOException;

/**
 *
 * @author Jannes
 */
public class RotationTextureBillboardControl extends BillboardControl{
    protected int textureAngles;
    private float currentTexture = 0;
    private float[] toAngles = new float[3];
    
    public RotationTextureBillboardControl(int textureAngles){
        super();
        this.textureAngles = textureAngles;
    }
    
    public RotationTextureBillboardControl(){
        super();
    }
    
@Override
    public Control cloneForSpatial(Spatial spatial) {
        RotationTextureBillboardControl control = new RotationTextureBillboardControl();
        control.setAlignment(getAlignment());        
        control.setSpatial(spatial);
        return control;
    }

@Override
    protected void controlRender(RenderManager rm, ViewPort vp) {
        super.controlRender(rm,vp);
	updateTextureCoord();
    }

    protected void updateTextureCoord(){
	spatial.getLocalRotation().toAngles(toAngles);                        
        int textureNumber = (int)(toAngles[1] * textureAngles /FastMath.TWO_PI);	
        if(textureNumber < 0)
            textureNumber += textureAngles;
        if(currentTexture != textureNumber){
            System.out.println("angle: " + toAngles[1]);
            System.out.println("textureNumber: " + textureNumber);
            try{                            
                float xStart = 1.0f*textureNumber/textureAngles;
                float xEnd = 1.0f*(textureNumber+1)/textureAngles;
                Vector2f[] texBuf = new Vector2f[4];
                texBuf[0] = new Vector2f(xStart,0);
                texBuf[1] = new Vector2f(xEnd,0);
                texBuf[2] = new Vector2f(xEnd,1);
                texBuf[3] = new Vector2f(xStart,1);
                ((Geometry)spatial).getMesh().getBuffer(Type.TexCoord).updateData(
                        BufferUtils.createFloatBuffer(texBuf));                                        
                currentTexture = textureNumber;                
                System.out.println("texture updated");
            }catch(Exception exc){
                System.out.println("error changing texcoord");
            }
        }
    }

    @Override
    public void write(JmeExporter e) throws IOException {
        super.write(e);
        OutputCapsule capsule = e.getCapsule(this);        
        capsule.write(textureAngles, "textureAngles", 4);
    }

    @Override
    public void read(JmeImporter e) throws IOException {
        super.read(e);
        InputCapsule capsule = e.getCapsule(this);        
        textureAngles = capsule.readInt("textureAngles", 4);
    }

}
